<?php

namespace app\service\home;

use Exception;
use PHPExcel_Exception;
use PHPExcel_Reader_Excel2007;
use PHPExcel_Reader_Exception;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\facade\Db;
use think\facade\Filesystem;

/**
 * 上传服务层
 * Class UploadService
 * @package projects\services\home
 * @author Du
 * @date 2021/3/1
 * @time 14:56
 */
class UploadService
{
    /**
     * 上传是否保存本地
     * @var bool
     */
    static bool $isLocal = true;

    /**
     * 限制上传图片类型
     * @var string[]
     */
    static array $imgExt = ['jpg', 'jpeg', 'gif', 'png'];

    /**
     * 限制上传图片大小
     * @var float|int
     */
    static $imgSize = 1024*1204;
//    static $imgSize = 1024*1204*5;

    /**
     * 上传图片保存本地
     * @author Du
     * @date 2021/3/3 10:42
     */
    static public function imgUpload()
    {
        $file = request()->file('file');
        $ext = $file->getOriginalExtension();  // 原始文件后缀名
        $size = $file->getSize();              // 原始文件大小
        if (!in_array($ext, self::$imgExt)) return -1;
        if ($size <= 0 || $size >= self::$imgSize) return -2;
        if (self::$isLocal){
            try{
                $saveName = Filesystem::disk('public')->putFile('upload', $file, 'md5');
                $res['url'] = $saveName;
                $res['src'] = isHTTPS().$_SERVER['SERVER_NAME'].'/'.$saveName;
                return $res;
            }
            catch (Exception $e){
                return false;
            }
        }else{
            try{
                $tmpName = $file->getRealPath();  // 临时文件路径
                $auth = new Auth(config('qiniu.accessKey'), config('qiniu.secretKey'));   // 初始化鉴权对象
                $token = $auth->uploadToken(config('qiniu.bucket'));  // 生成上传Token
                $uploadMgr = new UploadManager();  // 上传管理类 构建UplaodManager对象
                // 随机文件名
                $path = '/'. md5(microtime(true) . mt_rand(1, 1e9)) . '.' . $ext;
                $info = $uploadMgr->putFile($token, $path, $tmpName);
                $res['url'] = $info[0]['key'];
                $res['src'] = config('qiniu.domain').'/'.$res['url'];
                return $res;
            }
            catch (Exception $e){
                return false;
            }
        }
    }

    /**
     * excel导入
     * @throws PHPExcel_Exception
     * @throws PHPExcel_Reader_Exception
     * @author Du
     * @date 2021/3/2 18:17
     */
    static public function excelUpload()
    {
        try{
            $file = request()->file('file');
            $path = $file->getRealPath();
            $phpExcel = new PHPExcel_Reader_Excel2007();
            $objPHPExcel = $phpExcel->load($path);  //$filename可以是上传的表格，或者是指定的表格
            $excel_array = $objPHPExcel->getSheet(0);   //转换为数组格式
            $allColumn = $excel_array->getHighestColumn(); // 获取总列数
            $highestRow = $excel_array->getHighestRow();
            $data = [];
            for ($j = 1; $j <= $highestRow; $j++) {
                //从A列读取数据
                for ($k = 'A'; $k <= $allColumn; $k++) {
                    // 读取单元格
                    $data[$j][] = $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue();
                }
            }
            cache('seoList', $data);
            return $data;
            //        foreach ($data as $k => $v) {
//            if ($k > 1) {
//                $info = [
//                    'extend_name' => '巧匠-PS',
////                    'extend_name' => '巧匠-PR',
////                    'extend_name' => '巧匠-C4D',
////                    'extend_name' => '巧匠-AU',
////                    'extend_name' => '巧匠-AI',
////                    'extend_name' => '巧匠-AE',
////                    'extend_name' => '巧匠-3DMAX',
//                    'keyword' => $v[1],
//                    'extend_unit_name' => $v[0],
//                    'default_access_url' => $v[2]
//                ];
//                $arr[] = $info;
//            }
//        }
//        dd($arr);
//        Db::name('qj_extend_info')->data($arr)->insertAll();
//        echo 'oko';die;
        }
        catch (Exception $e){
            return false;
        }
    }
}